package com.mer.member.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.member.entity.Member;
import com.mer.member.mapper.MemberMapper;
import com.mer.member.utils.Result;
import com.mer.member.vo.MemberVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: iiphase-project-master
 * @description:
 * @author: 薛宁宁
 * @create: 2021-10-19 15:15
 **/

@Service
public class MemberImpl implements MemberService{
    @Resource
    private MemberMapper memberMapper;
    @Override
    public Result queryAllMember(Integer page, Integer limit, MemberVo memberVo) {
        //分页插件
        Page<Member> page1=new Page<>(page,limit);
        //查询条件
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(memberVo.getMobile())){
            wrapper.like("mobile",memberVo.getMobile());
        }
        if (StringUtils.isNotEmpty(memberVo.getNickname())){
            wrapper.like("nickname",memberVo.getNickname());
        }
        if (StringUtils.isNotEmpty(memberVo.getIsDisabled())){
            wrapper.eq("is_disabled",memberVo.getIsDisabled());
        }
        if (memberVo.getGmtCreate().size()!=0){
            wrapper.ge("gmt_create",memberVo.getGmtCreate().get(0));
            wrapper.le("gmt_create",memberVo.getGmtCreate().get(1));
        }
        //查询条件
        Page<Member> memberPage = memberMapper.selectPage(page1, wrapper);
        return new Result(2000,"查询成功",memberPage);
    }

    @Override
    public Result modifyMember(Member member) {
        member.setGmtModified(LocalDateTime.now());
        int i = memberMapper.updateById(member);
        if (i>0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }

    @Override
    public Result changeState(Boolean isDisabled, String id) {
        Member member = new Member();
        member.setId(id);
        member.setIsDisabled(isDisabled);
        int i = memberMapper.updateById(member);
        if (i>0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }
}
